package com.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.entity.History;
import org.python.core.PyFunction;
import org.python.core.PyInteger;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
import org.springframework.util.ResourceUtils;

import java.io.FileNotFoundException;
import java.util.*;

public class RecommendUtil {


    public static List<String> getRecommends(String userid, List<History> historyList, int K, int N) throws FileNotFoundException {

        String historyJson = JSON.toJSONString(historyList);

        PythonInterpreter interpreter = new PythonInterpreter();
        String path = ResourceUtils.getURL("classpath:").getPath()+"static/py/recommend.py";
        interpreter.execfile(path);

        PyFunction pyFunction = interpreter.get("getRecommendCommodities", PyFunction.class);
        PyObject pyObj = pyFunction.__call__(new PyString(userid), new PyString(historyJson), new PyInteger(K), new PyInteger(N));

        List<String> recommendsId = JSON.parseObject(pyObj.toString(), new TypeReference<ArrayList<String>>(){});
        return recommendsId;

    }


    /*//获取用户对商品的浏览信息，存入user_com
    *//*public List<Map<String, Map<String, >>> getData(){
        //user_movie = Map<String uid, Map<String mid, int score>>
        //
    }*//*
    private Map<String, Map<String, Integer>> user_commodity = new HashMap<>();
    private Map<String, Commodity> commodityMap = new HashMap<>();

    //数据处理
    public void setUser_commodity(List<History> historyList) {
        for(History history : historyList){
            user_commodity.put(history.getUserid(),new HashMap<>());
            user_commodity.get(history.getUserid()).put(history.getCommid(), history.getViews());
        }
    }

    public void setCommodityMap(List<Commodity> commodityList){
        for (Commodity commodity: commodityList) {
            commodityMap.put(commodity.getCommid(), commodity);
        }
    }

    public Map<String, Map<String, Integer>> getUser_commodity() {
        return user_commodity;
    }


    public Map<String, Commodity> getCommodityMap() {
        return commodityMap;
    }

    //计算相似度矩阵W
    //W = Map<String movieId, Map<String movieId, int viewedPerson>>
    public Map<String, Map<String, Double>> itemSimilarity(Map<String, Map<String, Integer>> user_commodity){
        Map<String, Map<String, Integer>> C = new HashMap<>();//存放最终的物品相似度矩阵
        Map<String, Integer> N = new HashMap<>();//存放每个商品的评分人数
        for(Map<String, Integer> commodityViews : user_commodity.values()){
            for(String commid : commodityViews.keySet()){
                //hashmap.merge(key, value, remappingFunction)
                //如果 key 对应的 value 不存在，则返回该 value 值，如果存在，则返回通过 remappingFunction 重新计算后的值。
                N.merge(commid, 1, Integer::sum);
                C.merge(commid, null, null);
                for(String commid2 : commodityViews.keySet()){
                    if(commid == commid2){
                        continue;
                    } else {
                        C.get(commid).merge(commid2, 1, Integer::sum);
                    }
                }
            }
        }

        Map<String, Map<String, Double>> W = new HashMap<>();
        for(String commid1 : C.keySet()){
            W.put(commid1, null);
            for(String commid2 : C.keySet()){
                Integer cij = C.get(commid1).get(commid2);
                Integer i = N.get(commid1);
                Integer j = N.get(commid2);
                Double wij = cij/Math.sqrt(i*j);
                W.get(commid1).put(commid2, wij);
            }
        }
        return W;
    }
    //4、计算推荐结果 K表示取K个相似的用户进行计算，N表示最终得到对目标用户的N个推荐结果
    public List<Integer> recommend(String userid,
                                     Map<String, Map<String, Integer>> user_commodity,
                                     Map<String, Map<String, Double>> W,
                                     int K, int N){
        List<Integer> commidList = new ArrayList<>();
        for(Map<String, Integer> commViews: user_commodity.values()){
            TreeMap itemW = (TreeMap) W.get(commViews.values()).keySet();
            for( String commid : ){

            }
        }
        return null;

    }*/
}
